<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>防抖</title>
</head>
<body>
  <input type="button" value="没有防抖处理" class="btn1">
  <input type="input"  class="btn2">
  <input type="button" value="防抖处理(立即执行)" class="btn3">
</body>
<script>
  var btn1 = document.querySelector(".btn1")
  var btn2 = document.querySelector(".btn2")
  var btn3 = document.querySelector(".btn3")
  function fun1(){
    console.log('没有防抖处理');
  }
  //延时执行(防抖)
  function debounce(fun,delay){
    let timer;
    return function debounced(){
      if(timer != null){
        clearTimeout(timer)
      } 
      var _this = this
      timer = setTimeout(function(){
        fun.apply(_this)
      },delay)
    }
  }
  btn2.addEventListener('input',debounce(function(){
    console.log(this.value)
  },500))
</script>
</html>